package com.yxkj.syh.app.huarong.activities.user;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.syh.app.basic.BR;
import com.syh.app.basic.base.BaseFragment;
import com.syh.app.basic.utils.ActivityManager;
import com.syh.app.basic.utils.Tooast;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.activities.account.login.LoginActivity;
import com.yxkj.syh.app.huarong.adps.AppointmentListAdp;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.FragmentUserBinding;

/**
 * 用户 个人
 */
public class UserFragment extends BaseFragment<FragmentUserBinding, UserVM> {

    private AppointmentListAdp mAppointmentListAdp;

    public UserFragment() {
        // Required empty public constructor
    }

    public static UserFragment newInstance() {
        UserFragment fragment = new UserFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.userVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_user;
    }

    @Override
    protected void initView() {

        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mVDBinding.flBack.getLayoutParams();
        layoutParams.topMargin = ImmersionBar.getStatusBarHeight(this);
        mVDBinding.flBack.requestLayout();
        mVDBinding.flBack.setOnClickListener(v -> getActivity().finish());

        mVDBinding.llCarInfo.setOnClickListener(v -> {
            if (TextUtils.isEmpty(mViewModel.ofCarNo.get())) {
                Tooast.warning("暂未绑定车辆");
            } else {
                ARouter.getInstance().build(ArouterPath.DRIVER_CAR_INFO_ACTIVITY).navigation();
            }
        });
        mVDBinding.btnLogout.setOnClickListener(v -> {
            mViewModel.logout();
        });

        mVDBinding.llNotice.setOnClickListener(v -> {
            ARouter.getInstance().build(ArouterPath.NOTICE_LIST_ACTIVITY).navigation();
        });
        mVDBinding.llPwd.setOnClickListener(v -> {
            ARouter.getInstance().build(ArouterPath.EDIT_PWD_ACTIVITY).navigation();
        });

    }

    @Override
    protected void initData() {
        mViewModel.userInfo();
        mViewModel.report();
    }


    @Override
    protected void visibleAgain() {
        initData();
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldLogout.observe(this, aBoolean -> {
            if (aBoolean) {
                ActivityManager.getInstance().finishAllActivity();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }
        });
    }


}
